<?php
class Business_Private extends Public_Controller{

	private $page_size = 10;
	private $_time;
	function __construct(){
		parent::__construct();
		$this->load->helper('admin');
		$this->load->helper('kunkun_page');
		$this->load->helper('admin_theme');
		//check quyen truong phong, giam doc
		$group = $this->current_user->group;
		if($group == 'boss1' || $group == 'boss11' || $group == 'boss2' || $group == 'boss3')
			$this->template->set('boss',$group);
		date_default_timezone_set('Asia/Ho_Chi_Minh');
		
		if($this->method != 'giaoviec' AND $this->method != 'edit' AND $this->method != 'create'){
			$this->template->append_js('f5.js');
		}
		//$this->_time =  $this->time_zone();
		$this->template->append_css('clock/clock.css')->append_js('clock/clock.js')->set('time_zone',$this->time_zone());
		//filter
		$this->load->model('list_m');
		$this->template->set('list_priority',$this->list_m->list_priority());// muc do uu tien
	}
	public function index(){
		$this->load->model('private_m');
		
		$base_where = array('user_id'=>$this->current_user->id);
		$base_where = $this->input->get('f_page') ? $base_where + array('limit' => $this->input->get('f_page')) : $base_where;
		
		$model = $this->private_m->get_many_by($base_where,$this->page_size);
		$total_page = total_page($model->total_row,$this->page_size);	
		$this->template
			->set('model',$model->projects)
			->set('total_page',$total_page)
			
			->set('title',lang('project:private'))->title(lang('project:private'))
			->append_js('chosen/chosen.jquery.min.js')
			->append_js('business/site.js')
			->append_css(array('chosen/chosen.css','table2.css'))
			->append_js('admin/scripts.js')
			->append_js('filter.js');
		if ($this->input->is_ajax_request())
		{
			$this->template->set_layout(false);
		}
		$this->input->is_ajax_request() ? $this->template->build($this->controller.'/partial/list') : $this->template->build($this->controller.'/index');
	}
	public function create(){
		$this->action = 1;
		$model = $this->input->post();
		
		$this->form_validation->set_rules('title', lang('project:title'), 'required|max_length[255]');
		$type =  substr($this->input->post('btnAction'), 0);
		if ( type_post($type) ) {
			if ($this->form_validation->run() == TRUE)
			{
				$skip = array('btnAction','id');
				foreach($skip as $obj){
				if(array_key_exists($obj,$model))
					unset($model[$obj]);
				}
				$this->load->model('private_m');
				$model['room_id'] = $this->current_profile->room_id;
				$this->private_m->insert($model);
				redirect('business/private');
			}
		}
		$this->template
			->set('title',lang('project:private:create'))->title(lang('project:private:create'))
			->set(array('model'=>$model,'action'=>$this->action))
			->append_css(array('table.css','form_input.css','error.css','ui/themes/redmond/jquery-ui.min.css'))
			->append_js(array('action/action.js','ckeditor/ckeditor.js','ui/jquery-ui.min.js'))
			->build($this->controller.'/form');
	}
	
	public function view($id){
		$this->load->model('private_m');
		$model = $this->private_m->get($id);
		//test
		//$date = strtotime(date('Y-m-d H:i:s'));
		//echo $date;exit;
		
		/*
		$i = array();
		$i1 = array('id'=>'1384134056','note'=>"notes 1");
		$i2 = array('id'=>'1384134085','note'=>"notes 2");
		$i[] = $i1;
		$i[] = $i2;		
		$notes = $i;
		
		echo json_encode($notes);exit;
		//echo json_encode($notes,JSON_FORCE_OBJECT);exit;
		*/
		
		$notes = json_decode($model->notes);	
		$this->template
			->title($model->title)
			->set('title',$model->title)
			->append_js('jquery-te/jquery-te-1.4.0.min.js')
			->append_css('jquery-te/jquery-te-1.4.0.css')
			->append_css('comment.css')
			->set('model',$model)
			->set('notes',$notes)
			->build($this->controller.'/view');
	}
	
	public function append_note(){
	
		//$this->security->xss_clean(array('style'));
		$json = array();
		
		$model = $this->input->post();
		
		$this->form_validation->set_rules('note', lang('global:content'), 'required');
		if ($this->form_validation->run() == TRUE)
		{
			$data = new stdClass();
			$data->id = strtotime(date('Y-m-d H:i:s'));
			if(!empty($model['file'])){
				$data->file = $model['file'];
			}
			$data->note = $model['note'];
			
			$this->load->model('private_m');
			$single = $this->private_m->get($model['private_id']);
			$notes = (array)json_decode($single->notes);
			array_unshift($notes, $data);//dua note len dau tien trong mang da co
			//$notes[] = $data;
			//print_r($notes);exit; 
			//[{"id":"1384134056","note":"notes 1"},{"id":"1384134085","note":"notes 2"},{"id":1384135743,"note":"123 34<br>"}]
			$notes_str = json_encode($notes);
			$this->private_m->update($model['private_id'],array('notes'=>$notes_str));
		}
		else{
			$json['error'] = validation_errors('<p class="error"> -', '</p>');
		}
		echo json_encode($json);
		
		
	}
	
	public function finish($id){
		$this->load->model('private_m');
		$this->private_m->update($id,array('status'=>true));
		redirect('business/private/view/'.$id);
	}
}